package inporter;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Hashtable;

import main.TokenArrayProcessor;
import main.tokens;

public class TokenInporter 
{
	private ArrayList<tokens> arlTokenAll = new ArrayList<tokens>();
	private TokenArrayProcessor clsTAProcessor = new TokenArrayProcessor(arlTokenAll);
	
	//Contains each line of code
	private ArrayList<String> arlCodeFileLine = new ArrayList<String>();
	//Contains each line of file
	private ArrayList<String> arlFileLine = new ArrayList<String>();
	
	
	/*
	 * Function: Get each line from code file
	 * Input: pFileLocation -> File location
	 * Output: None
	 */
	public ArrayList<tokens> arlInporter(String pFileLocation)
	{
		try
		{
			FileReader reader = new FileReader(pFileLocation);
			BufferedReader br = new BufferedReader(reader);
			String strCntLine = null;
			//Record current line, start with 1
			int intCntLine = 1;
			 
			//For each line, call function [tokenReorganization] to wipe off duplicate tokens
			//then call function [clsTAProcessor.setTokens] to record tokens
			while((strCntLine = br.readLine()) != null) 
			{
				clsTAProcessor.initialIndex();
				clsTAProcessor.setTokens(tokenReorganization(strCntLine), intCntLine);
				
				arlCodeFileLine.add(strCntLine);
				intCntLine ++;
			}
			arlTokenAll = clsTAProcessor.getTokensArray();
			
			br.close();
			reader.close();
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
		
		return arlTokenAll;
	}
	
	/*
	 * Function: process each line, to wipe off duplicate tokens
	 * Input: pCntLine -> current line
	 * Output: tokens in current line without repetition
	 */
	private ArrayList<String> tokenReorganization(String pCntLine)
	{
		ArrayList<String> arlCntLine = new ArrayList<String>();
		
		String strTokenTemp[] = pCntLine.split(" ");
		for (int i = 0; i < strTokenTemp.length; i++)
		{
			if (!arlCntLine.contains(strTokenTemp[i].trim()))
			{
				arlCntLine.add(strTokenTemp[i].trim());
			}
		}
		
		return arlCntLine;
	}
	
	/*
	 * Function: Return code file ArrayList
	 * Input: None
	 * Output: code file ArrayList
	 */
	public ArrayList<String> getCodeArray()
	{
		return arlCodeFileLine;
	}
	
	/*
	 * Function: Return an ArrayList which contains each line of file
	 * Input: pFileLocation -> file location
	 * Output: arlFileTokens -> ArrayList which contains each line of file
	 */
	public ArrayList<String> arlGetFileLines(String pFileLocation)
	{
		try
		{
			FileReader reader = new FileReader(pFileLocation);
			BufferedReader br = new BufferedReader(reader);
			String strCntLine = null;
			
			while((strCntLine = br.readLine()) != null) 
			{
				arlFileLine.add(strCntLine);
			}			
			br.close();
			reader.close();
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return arlFileLine;
	}
	
	/*
	 * Function: Get tokens from file line ArrayList, delete the same one, and sort them
	 * Input: None
	 * Output: arlFileTokens -> ArrayList which contains all file tokens  
	 * 
	 * Update(13.12.01): change ArrayList to Hash table
	 */
	public Hashtable<String, String> getFileToken()
	{
		Hashtable<String, String> htFileTokens = new Hashtable<String, String>();
		
		for (int i = 0; i < arlFileLine.size(); i++)
		{
			String[] strTmpTokens = arlFileLine.get(i).split(" ");
			for (int j =0; j < strTmpTokens.length; j++)
			{
				if (!htFileTokens.containsKey(strTmpTokens[j].trim()))
				{
					htFileTokens.put(strTmpTokens[j], strTmpTokens[j]);
				}
			}
		}
		
		return htFileTokens;
	}
}

